package com.ordern.classification;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

import org.apache.log4j.Logger;

public class ContestDriver {

	private static Logger logger = Logger.getLogger(ContestDriver.class);

	public List<Domain> getTrainSet(String testDataBaseDir,
			String testDataFileName) throws Exception {
		long startRead = System.currentTimeMillis();
		FileReader testFileReader = new FileReader(testDataBaseDir + "/"
				+ testDataFileName);
		List<Domain> domains = new InputParser().parseInput(testFileReader,
				testDataBaseDir);

		return domains;
	}

	public List<Domain> getBoosterTrainSet(String boosterDirectory)
			throws Exception {
		return TestDataDriver.getBoosterDomains(boosterDirectory);
	}

	public void runContest(InputStream input, OutputStream output,
			String baseDir, CustomClassifier classifier) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				output));
		List<Domain> domains = new InputParser().parseInput(reader, baseDir);
		logger.info("Input domain size " + domains.size());
		long startTest = System.currentTimeMillis();
		classifier.test(domains);
		long endTest = System.currentTimeMillis();
		long testTime = endTest - startTest;
		if (logger.isDebugEnabled()) {
			for (Domain domain : domains) {
				logger.debug(domain);
			}
		}

		for (Domain domain : domains) {
			writer
					.write(domain.getName()
							+ ":"
							+ ((domain.getDomainType()
									.equals(DomainType.PERSONAL)) ? "personal"
									: "not personal"));
			writer.newLine();
		}
		writer.flush();
		writer.close();

	}

}
